<template>
  <basic-dialog
    :visible="dialogVisible"
    :title="'原材料-处理意见审核'"
    :width="'60%'"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpenDialog"
    :showFooter="false"
  >
    <el-form
      ref="auditForm"
      :model="auditForm"
      label-width="180px"
      v-loading="formLoading"
    >
      <el-form-item label="原材料浮动:">
        <span>{{auditForm.currentFloatRadioStr}} {{auditForm.currentFloatPrice}}</span>
      </el-form-item>
      <el-form-item :label="auditForm.currentFloatPrice > 0 ? '是否需调整售价:' : '是否需调整进价:'" prop="currentAdjustPriceStateStr">
        <span>{{auditForm.currentAdjustPriceStateStr}}</span>
      </el-form-item>
      <el-form-item label="处理备注说明:" prop="currentProcessRemark">
        <el-input disabled v-model.trim="auditForm.currentProcessRemark" type="textarea" :rows="2" />
      </el-form-item>
      <el-form-item label="连续预警未调价次数:">
        <span>{{auditForm.currentUnAdjustPriceTimes}}</span>
      </el-form-item>
      <el-form-item label="上次材料浮动:">
        <span>{{auditForm.floatRadioStr}} {{auditForm.floatPrice}}元</span>
      </el-form-item>
      <el-form-item label="上次是否需调整售价/进价:">
        <span>{{auditForm.adjustPriceStateStr}}</span>
      </el-form-item>
      <el-form-item label="上次处理备注说明:">
        <span>{{auditForm.processRemark}}</span>
      </el-form-item>
      <el-form-item label="上次处理人:">
        <span>{{auditForm.operName}}</span>
      </el-form-item>
      <el-form-item label="上次处理时间:">
        <span>{{auditForm.opeTime}}</span>
      </el-form-item>
      <el-form-item label="最后一次调整进价时间:">
        <span>{{auditForm.adjustPriceTime}}</span>
      </el-form-item>
      <el-form-item label="审核说明:" prop="approveRemark">
        <el-input v-model.trim="auditForm.approveRemark" maxlength="200" type="textarea" :rows="2" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button :loading="auditLoading" @click="handleAudit(2)">审核驳回</el-button>
      <el-button type="primary" @click="handleAudit(1)">审核通过</el-button>
    </el-row>
  </basic-dialog>
</template>
<script>
import { setAuthBtnId } from '@/utils/bizUtils';
import BasicDialog from '@/components/BasicDialog';
import { PRICE_STATE_OPTIONS } from '../constants'
import { queryMaterialToDoDetailService, handleApproveMaterialService } from '@/api/finance/rawMaterialManage/toDoList';

export default {
  name: 'AuditDialog', // 审核
  components: {
    BasicDialog,
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    todoId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      formLoading: false,
      auditLoading: false,
      PRICE_STATE_OPTIONS,
      auditForm: {
        currentFloatRadio: '',
        currentFloatPrice: '',
        currentAdjustPriceState: '',
        currentProcessRemark: '',
        currentUnAdjustPriceTimes: '',
        floatRadio: '',
        floatPrice: '',
        adjustPriceStateStr: '',
        processRemark: '',
        operName: '',
        opeTime: '',
        adjustPriceTime: '',
        approveRemark: '',
      },
    };
  },
  computed: {
    dialogVisible () {
      return this.visible;
    },
    uuid () {
      return this.todoId;
    }
  },
  methods: {
    async handleAudit (auditType) {
      const { approveRemark } = this.auditForm
      if (auditType == 2 && !approveRemark) {
        return this.$message.warning('审核驳回须填写审核说明')
      }
      const data = {
        uuid: this.uuid,
        approveRemark,
        approveState: auditType // 1审核通过、2审核不通过
      }
      const res = await handleApproveMaterialService(
        data,
        state => { this.auditLoading = state }
      );
      if (res) {
        this.$message.success('提交成功')
        this.$emit('getTableData')
        this.handleCloseDialog()
      }
    },
    // 获取详情数据
    async getDetailData () {
      setAuthBtnId();
      const data = {
        uuid: this.uuid,
        rawMaterialProcessType: 2 // 处理
      };
      const res = await queryMaterialToDoDetailService(
        data,
        state => { this.formLoading = state }
      );
      if (res?.retData) {
        this.auditForm = res.retData;
        this.auditForm.approveRemark = '';
      }
    },
    // 打开弹窗
    handleOpenDialog () {
      this.getDetailData()
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
  }
};
</script>
